using System;
using System.Collections;

namespace THANHTUNGO.Portal.Components.Settings
{
    public class PortalSettings
    {
        private Tab _ActiveTab = new Tab();
        private bool _AlwaysShowEditButton;
        private int _ID;
        private string _Name;
        private string _MasterFile = "Office";
        private string _Language = "en-US";
        private bool _AllowRegister = true;

        
        private ArrayList _Tabs = new ArrayList();

        public PortalSettings(int tabIndex, int tabId)
        {
            try
            {
                // Get the configuration data
                PortalConfigurations siteSettings = Configuration.GetPortalConfigurations();

                // Read the Desktop Tab Information, and sort by Tab Order
                foreach (PortalConfigurations.TabRow tRow in siteSettings.Tab.Select("", "Order"))
                {
                    Tab tab = new Tab();
                    tab.ID = tRow.ID;
                    tab.Name = tRow.Name;
                    tab.AuthorizedRoles = tRow.AuthorizedRoles;
                    tab.Order = tRow.Order;
                    tab.ThemeName = tRow.ThemeName;
                    tab.ShowLeftPane = tRow.ShowLeftPane;
                    tab.ShowRightPane = tRow.ShowRightPane;
                    tab.ShowOnMenu = tRow.ShowOnMenu;
                    this.Tabs.Add(tab);
                }

                // If the PortalSettings.ActiveTab property is set to 0, change it to  
                // the TabID of the first tab in the DesktopTabs collection
                if (this._ActiveTab.ID == 0)
                    this._ActiveTab.ID = ((Tab) this.Tabs[0]).ID;


                // Read the Module Information for the current (Active) tab
                PortalConfigurations.TabRow activeTab = siteSettings.Tab.FindByID(tabId);

                // Get Modules for this Tab based on the Data Relation
                foreach (PortalConfigurations.ModuleRow moduleRow in activeTab.GetModuleRows())
                {
                    Module module = new Module();
                    module.ShowTitle = moduleRow.ShowTitle;
                    module.Title = moduleRow.Title;
                    module.ID = moduleRow.ID;
                    module.ModuleDefinitionID = moduleRow.ModuleDefinitionID;
                    module.Order = moduleRow.Order;
                    module.TabID = tabId;
                    module.PaneName = moduleRow.PaneName;
                    module.AuthorizedViewRoles = moduleRow.AuthorizedViewRoles;
                    module.AuthorizedAddRoles = moduleRow.AuthorizedAddRoles;
                    module.AuthorizedEditRoles = moduleRow.AuthorizedEditRoles;
                    module.AuthorizedDeleteRoles = moduleRow.AuthorizedDeleteRoles;
                    module.AuthorizedPropertyRoles = moduleRow.AuthorizedPropertyRoles;
                    module.AuthorizedManagerRoles = moduleRow.AuthorizedManagerRoles;
                    module.CacheTimeOut = moduleRow.CacheTimeout;

                    // ModuleDefinition data.
                    PortalConfigurations.ModuleDefinitionRow modDefRow = siteSettings.ModuleDefinition.FindByID(module.ModuleDefinitionID);
                    module.SourceFile = modDefRow.SourceFile;
                    module.GeneralDefinitionID = modDefRow.GeneralDefinitionID;
                    this.ActiveTab.Modules.Add(module);
                }

                // Sort the modules in order of ModuleOrder
                this.ActiveTab.Modules.Sort();

                // Get the first row in the Global table
                PortalConfigurations.GlobalRow globalSettings = (PortalConfigurations.GlobalRow) siteSettings.Global.Rows[0];

                // Read Portal global settings 
                this._ID = globalSettings.PortalID;
                this._Name = globalSettings.PortalName;
                this._AlwaysShowEditButton = globalSettings.AlwaysShowEditButton;
                this._MasterFile = globalSettings.MasterFile;
                this._Language = globalSettings.Language;
                this._AllowRegister = globalSettings.AllowRegister;
                
                this._ActiveTab.Index = tabIndex;
                this._ActiveTab.ID = tabId;
                this._ActiveTab.Name = activeTab.Name;
                this._ActiveTab.AuthorizedRoles = activeTab.AuthorizedRoles;
                this._ActiveTab.Order = activeTab.Order;
                this._ActiveTab.ThemeName = activeTab.ThemeName;
                this._ActiveTab.ShowLeftPane = activeTab.ShowLeftPane;
                this._ActiveTab.ShowRightPane = activeTab.ShowRightPane;
                this._ActiveTab.ShowOnMenu = activeTab.ShowOnMenu;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// Portal ID.
        /// </summary>
        public int ID
        {
            get { return this._ID; }
            set { this._ID = value; }
        }

        /// <summary>
        /// Portal Name.
        /// </summary>
        public string Name
        {
            get { return this._Name; }
            set { this._Name = value; }
        }

        public bool AlwaysShowEditButton
        {
            get { return this._AlwaysShowEditButton; }
            set { this._AlwaysShowEditButton = value; }
        }

        public ArrayList Tabs
        {
            get { return this._Tabs; }
            set { this._Tabs = value; }
        }

        public Tab ActiveTab
        {
            get { return this._ActiveTab; }
            set { this._ActiveTab = value; }
        }

        public string MasterFile
        {
            get { return this._MasterFile; }
            set { this._MasterFile = value; }
        }

        public string Language
        {
            get { return this._Language; }
            set { this._Language = value; }
        }

        public bool AllowRegister
        {
            get { return this._AllowRegister; }
            set { this._AllowRegister = value; }
        }
    }
}